<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index"></el-table-column>
          <el-table-column align="center" header-align="center" label="信息标题" prop="title"></el-table-column>
          <el-table-column align="center" header-align="center" label="所属分类" prop="cate_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="所属地区" prop="address"></el-table-column>
          <el-table-column align="center" header-align="center" label="前台状态" prop="sts_show"></el-table-column>
          <el-table-column align="center" header-align="center" label="推进人数" prop="push_num"></el-table-column>
          <el-table-column align="center" header-align="center" label="发布时间" prop="send_time"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDetail(scope)">查看</el-button>
              <el-button size="mini" type="text" @click="handlePoster(scope)">生成宣传海报</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  详情弹窗 -->
    <el-drawer title="推送详情" :visible.sync="showDetail" :destroy-on-close="true" size="60%">
      <PushDetail @showCustomerInfo="handleShowCustomerInfo" :sts-list="stsList" :id="currentId" :type="showDialogType" :urlPostId="urlPostId" :urlVipLevel="urlVipLevel" @close="showDetail = false" @refresh="handleRefresh" />
    </el-drawer>

    <!--  生成宣传海报-->
    <el-dialog title="生成宣传海报" :visible.sync="showPoster" top="10px" width="80%">
      <PushPoster v-if="showPoster" :sts-list="stsList" :id="currentId" :options="zoneSelect" @close="showPoster = false" />
    </el-dialog>

    <!--  目标客户-->
    <el-drawer title="推送详情" :visible.sync="showCustomerInfo" :destroy-on-close="true" size="90%">
      <CustomerInfo @edit="handleCustomerEdit" @audio="handleShowAudio" :id="currentId" :urlRecordId="urlRecordId" :type="showDialogType" :urlPostId="urlPostId" :urlVipLevel="urlVipLevel" :vip-level="vipLevel" :vip-name="vipName" @close="showCustomerInfo = false" :sts-list="stsList" />
    </el-drawer>

    <!--  修改客户偏好-->
    <el-drawer title="查看" :visible.sync="showOfflineSignView" :destroy-on-close="true" size="40%">
      <OfflineSignView :id="customerId" @close="showOfflineSignView = false" @refresh="handleRefresh" />
    </el-drawer>

    <!--  查看录音-->
    <el-drawer title="查看通话录音" :visible.sync="showAudio" :destroy-on-close="true" size="24%">
      <record v-if="showAudio" :audioInfo="audioInfo" showType="audio-only" />
    </el-drawer>
  </div>
</template>

<script>
import { apiPushList, apiSignCateSelect, apiSignZoneSelect } from '@/api/assetsDisposal'
import PushDetail from './components/PushDetail'
import PushPoster from './components/PushPoster'
import CustomerInfo from './components/CustomerInfo'
import OfflineSignView from '../contractCustomer/components/OfflineSignView'
import record from '../../outboundCall/outCallFlow/record'

export default {
  name: 'index',
  components: { PushDetail, PushPoster, CustomerInfo, OfflineSignView, record },
  data() {
    const stsList = [
      { id: 0, text: '处置中' },
      { id: 1, text: '成交待核实' },
      { id: 2, text: '已成交' },
      { id: 3, text: '已下架' },
    ]
    return {
      stsList: stsList,
      searchFormArr: [
        {
          fieldName: '信息标题',
          type: 'input',
          field: 'title',
          placeholder: '请输入信息标题',
        },
        {
          fieldName: '信息类型',
          type: 'select',
          field: 'source_id',
          option: [
            { id: 2, text: '全部' },
            { id: 0, text: '资产' },
            { id: 1, text: '独享' },
          ],
          placeholder: '请选择',
        },
        {
          fieldName: '前台状态',
          type: 'select',
          field: 'front_sts',
          option: stsList,
          placeholder: '请选择',
        },
        {
          fieldName: '所属分类',
          type: 'cascader',
          field: 'cate_ids',
          option: [],
          placeholder: '请选择',
          props: { value: 'num_id', label: 'name' },
        },
        {
          fieldName: '所属地区',
          type: 'cascader',
          field: 'zone_ids',
          option: [],
          placeholder: '请选择',
          props: { value: 'code', label: 'name', checkStrictly: true },
        },
        {
          fieldName: '发布时间',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择创建时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
        {
          fieldName: '只看与我有关',
          type: 'checkbox',
          field: 'just_see_me',
        },
      ],
      isAccording: true,
      searchParams: {
        title: '', // 标题
        source_id: '', // 资产类型
        cate_ids: [], // 分类
        zone_ids: [], // 地区
        front_sts: '', // 0 处置中 1 成交待核实 2 已成交 3 已下架
        just_see_me: 0, // 只看我默认0 选中1
        start_time: '',
        end_time: '',
      },

      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      editMode: 'add',
      currentId: '',
      showDetail: false,
      showPoster: false,
      showCustomerInfo: false,
      vipLevel: '',
      vipName: '',
      customerId: '',
      showOfflineSignView: false,
      audioInfo: '',
      showAudio: false,
      zoneSelect: {},
      // url携带的id参数，直接跳转详情
      urlRecordId: '',
      urlPostId: '',
      urlVipLevel: '',
      showDialogType: 'click',
    }
  },
  mounted() {
    this.init()
    this.getDataList()
    this.getCateSelect()
    this.getZoneSelect()
  },
  methods: {
    // 初始化，获取参数直接打开弹窗
    init() {
      const { post_id, record_id, level } = this.$route.query
      if (post_id && record_id && (level || level === '0')) {
        this.currentId = this.urlPostId = post_id
        this.urlRecordId = record_id
        this.urlVipLevel = level
        this.showDialogType = 'url'
        this.showDetail = true
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams)
      info.just_see_me = info.just_see_me ? 1 : 0
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiPushList(formData)
        .then((res) => {
          const { items, total, url } = res.result
          items.forEach((item) => {
            item.sts_show = this.stsList.find((sts) => sts.id === item.sts).text
          })
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    getCateSelect() {
      apiSignCateSelect().then((res) => {
        if (res.code === 200) {
          const { data } = res
          data.forEach((item1) => {
            if (item1.children && item1.children.length === 0) {
              delete item1.children
            } else {
              item1.children.forEach((item2) => {
                if (item2.children && item2.children.length === 0) {
                  delete item2.children
                }
              })
            }
          })
          this.searchFormArr.find((item) => item.field === 'cate_ids').option = data
        }
      })
    },
    getZoneSelect() {
      const info = {
        user_id: this.userId,
      }
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiSignZoneSelect().then((res) => {
        if (res.code === 200) {
          const { data } = res
          data.forEach((item1) => {
            if (item1.children && item1.children.length === 0) {
              delete item1.children
            } else {
              item1.children.forEach((item2) => {
                if (item2.children && item2.children.length === 0) {
                  delete item2.children
                }
              })
            }
          })
          this.zoneSelect = data
          this.searchFormArr.find((item) => item.field === 'zone_ids').option = data
        }
      })
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field] || e[item.field] === 0) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 查看
    handleDetail(scope) {
      this.showDialogType = 'click'
      this.showDetail = true
      this.currentId = scope.row.id
    },
    // 生成海报
    handlePoster(scope) {
      this.editMode = 'poster'
      this.showPoster = true
      this.currentId = scope.row.id
    },

    // 刷新列表
    handleRefresh() {
      this.getDataList()
    },
    // 显示目标客户
    handleShowCustomerInfo({ level, name, type }) {
      this.vipLevel = level
      this.vipName = name
      this.showDialogType = type
      this.showCustomerInfo = true
    },
    // 用户偏好修改
    handleCustomerEdit(id) {
      this.customerId = id
      this.showOfflineSignView = true
    },
    // 查看录音
    handleShowAudio(info) {
      this.audioInfo = info
      this.showAudio = true
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.text-1 {
  .link {
    color: #409eff;
    cursor: pointer;
  }
}
</style>
